package net.wanglu.www.zzz.rule.artcl;

import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import net.wanglu.www.zzz.service.se.np._NPTuple;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.trees.Tree;

public class OnlyYou extends _ArticleRule {

	public Pattern OnlyYou = Pattern.compile("\\bsky\\b|\\bsea\\b|\\bglobe\\b|\\bEquator\\b|\\buniverse\\b|\\batmosphere\\b|\\bouter space\\b");
	@Override
	public Object applicable(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();
		if ( NP.depth() != 2) return Rule.Inapplicable;//如果不是叶子节点的父母则不符合要求
		TaggedWord headword = npt.getHeadword();
		if ( "NN".equals(headword.tag()) || "NNP".equals(headword.tag())) {
			return Rule.Applicable;//如果中心词是名词则符合要求，否则不符合
		} else {
			return Rule.Inapplicable;
		}
	}

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		if ( this.applicable(objects) == Rule.Inapplicable) return Rule.Inapplicable;
		
		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();

		/*
		 * 名词短语中
		 */
		sb.clear();
		for( TaggedWord tw : NP.taggedYield()) {
			sb.append(tw.word()).append(" ");
		}
		if ( !Rule.Ocean.matcher(sb.toString().trim()).find() && !Rule.Planet.matcher(sb.toString().trim()).find()) return Rule.Inapplicable;
		if ( !this.OnlyYou.matcher(sb.toString().trim()).find()) return Rule.Inapplicable;
		
		if ( sb.toString().trim().startsWith("the") || sb.toString().trim().startsWith("The")) {
			return null;
		} else {
			BadEgg egg = new BadEgg();
			egg.whereIsWrong = NP;
			egg.setProperty(BadEgg.Error);
			egg.setViolatedRule(this);
			return egg;				
		}
	}

	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.append("<p>");
		sb.append("表示独一无二的东西的名词，通常要加定冠词。");
		sb.append("</p>");			
		return sb.toString();
	}
}
